package com.pybox.spec;

import com.pybox.entity.TDictType;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
* 描述：TDictType Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TDictTypeSpec implements Specification<TDictType> {

    private Map<String, String> params;

    public TDictTypeSpec(Map<String, String> params){
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TDictType> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        String dictId = this.params.get("dictId");
        String dictName = this.params.get("dictName");
        String dictType = this.params.get("dictType");
        String status = this.params.get("status");
        String createBy = this.params.get("createBy");
        String createTime = this.params.get("createTime");
        String updateBy = this.params.get("updateBy");
        String updateTime = this.params.get("updateTime");
        String remark = this.params.get("remark");
        List<Predicate> ps = new ArrayList<>();

        if (!StringUtils.isEmpty(dictId)) {
            ps.add(criteriaBuilder.like(root.get("dictId"), "%" + dictId + "%"));
        }
        if (!StringUtils.isEmpty(dictName)) {
            ps.add(criteriaBuilder.like(root.get("dictName"), "%" + dictName + "%"));
        }
        if (!StringUtils.isEmpty(dictType)) {
            ps.add(criteriaBuilder.like(root.get("dictType"), "%" + dictType + "%"));
        }
        if (!StringUtils.isEmpty(status)) {
            ps.add(criteriaBuilder.like(root.get("status"), "%" + status + "%"));
        }
        if (!StringUtils.isEmpty(createBy)) {
            ps.add(criteriaBuilder.like(root.get("createBy"), "%" + createBy + "%"));
        }
        if (!StringUtils.isEmpty(createTime)) {
            ps.add(criteriaBuilder.like(root.get("createTime"), "%" + createTime + "%"));
        }
        if (!StringUtils.isEmpty(updateBy)) {
            ps.add(criteriaBuilder.like(root.get("updateBy"), "%" + updateBy + "%"));
        }
        if (!StringUtils.isEmpty(updateTime)) {
            ps.add(criteriaBuilder.like(root.get("updateTime"), "%" + updateTime + "%"));
        }
        if (!StringUtils.isEmpty(remark)) {
            ps.add(criteriaBuilder.like(root.get("remark"), "%" + remark + "%"));
        }

        criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        return criteriaQuery.getRestriction();
    }

}